package com.qcby.student.controller;

import com.qcby.student.entity.ResponseResult;
import com.qcby.student.entity.Student;
import com.qcby.student.mapper.LoginMapper;
import com.qcby.student.util.JwtUtil;
import com.qcby.student.util.VerifyCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class LoginController {

    @Autowired
    private LoginMapper loginMapper;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    /**
     * 获取验证码
     * @param response HTTP 响应对象
     */
    @RequestMapping("/login/verifyCode")
    public void generateVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("image/jpeg");

        VerifyCode verifyCode = new VerifyCode();
        BufferedImage image = verifyCode.getImage();
        String text = verifyCode.getText();

        HttpSession session = request.getSession();
        session.setAttribute("verifyCode", text);
        VerifyCode.output(image, response.getOutputStream());
    }

    /**
     * 管理员登录
     * @param student 登录对象（包含用户名、密码和验证码）
     * @param request HTTP 请求对象
     * @param response HTTP 响应对象
     * @return ResponseResult 登录结果
     */
    @RequestMapping(value = "/login/managerLogin", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult managerLogin(@RequestBody Student student, HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String sessionVerifyCode = (String) session.getAttribute("verifyCode");

        if (sessionVerifyCode == null || !sessionVerifyCode.equalsIgnoreCase(student.getImgcode())) {
            return new ResponseResult(999, "验证码错误");
        }

        List<Student> logins = loginMapper.studentLogin(student);
        if (logins.size() >= 1) {
            String token = JwtUtil.createJWT(UUID.randomUUID().toString(), String.valueOf(logins.get(0).getId()), null);
            Cookie cookie = new Cookie("studentToken", token);
            cookie.setPath("/");
            cookie.setMaxAge(36000);
            response.addCookie(cookie);

            // 保存student_id到session以备后续调用
            session.setAttribute("student_id", logins.get(0).getId());
            return new ResponseResult(200, "登录成功");
        } else {
            return new ResponseResult(444, "登录失败");
        }
    }
}
